/*
 * Copyright (C) 2010-2014 Enrique Lara (enrique.posix2.lara@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package trs.wmata;

import app.transittool.R;

import app.transittool.model.DataRetrievalException;
import app.transittool.model.Route;

import trs.wmata.api.Api;
import trs.wmata.api.RouteDetails;

class BusRoute implements Route {
    private static final String UNKNOWN = "UNKNOWN";
    private static final String[] UNKNOWN_ARR = new String[] {
                                                                 UNKNOWN
    };
    public static final Route UNKNOWN_ROUTE = new Route() {
        @Override
        public String getCode() {
            return UNKNOWN;
        }

        @Override
        public int getImageId() {
            return R.drawable.ic_transit_alarm;
        }

        @Override
        public String getName() {
            return UNKNOWN;
        }

        @Override
        public String[] getDirectionCodes() {
            return UNKNOWN_ARR;
        }

        @Override
        public String[] getDirectionNames() {
            return UNKNOWN_ARR;
        }
    };

    // public static BusRoute UNKNOWN = new BusRoute("UNKNOWN", "UNKNOWN",
    // R.drawable.ic_transit_alarm ) ;
    private final Api api;
    private final String code;
    private final int imageId;
    private String name;
    private String[] directionCodes = null;
    private String[] directionNames = null;

    BusRoute(final String code, final Api api) {
        super();
        this.api = api;
        this.code = code;
        name = code;
        imageId = R.drawable.ic_transit_alarm;
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String[] getDirectionCodes() {
        if (directionCodes == null) {
            try {
                initRouteDetails();
            } catch (final DataRetrievalException e) {
                return new String[] {
                           "UNKNOWN"
                       };
            }
        }

        return directionCodes;
    }

    @Override
    public String[] getDirectionNames() {
        if (directionNames == null) {
            try {
                initRouteDetails();
            } catch (final DataRetrievalException e) {
                return new String[] {
                           "UNKNOWN"
                       };
            }
        }

        return directionNames;
    }

    @Override
    public String getName() {
        if (name == null) {
            try {
                initRouteDetails();
            } catch (final DataRetrievalException e) {
                return code;
            }
        }

        return name;
    }

    private void initRouteDetails() throws DataRetrievalException {
        final RouteDetails routeDetails = api.getRouteDetails(code);
        directionCodes = routeDetails.getDirectionCodes();
        directionNames = routeDetails.getDirectionNames();
        name = routeDetails.getRouteName();
    }

    @Override
    public int getImageId() {
        return imageId;
    }
}
